package com.ruoyi.hr.controller.hr.salaryadmin.salaryopen;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.service.AuthorityService;
import com.ruoyi.hr.service.salaryadmin.salaryopen.SalaryOpenService;
import com.ruoyi.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 薪资开启
 */
@Controller
@RequestMapping("/salaryOpen/salaryOpen")
public class SalaryOpenController extends BaseController {

    @Autowired
    SalaryOpenService salaryOpenService;

    @Autowired
    AuthorityService authorityService;

    private String prefix = "hr/salaryadmin/salaryopen/salaryopen";

    /**
     * 薪资开启跳转初始化页面
     *
     * @return
     */
    @GetMapping()
    public String salaryOpen() {
        return prefix + "/salaryOpen";
    }

    /**
     * 薪资开启历史跳转初始化页面
     *
     * @return
     */
    @GetMapping("/salaryOpenHistory")
    public String salaryOpenHistory() {
        return prefix + "/salaryOpenHistory";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map map) {
        SysUser user = ShiroUtils.getSysUser();
        List<Authority> authorityList = authorityService.getAuthorityUser(user.getUserId().toString());
        int isAdmin = 0;
        if (authorityList.size() == 1) {
            if (authorityList.get(0).getIsAdmin() != null) {
                isAdmin = 1;
            }
        }
        startPage();
        return getDataTable(salaryOpenService.findData(map, authorityList, isAdmin));
    }

    @RequestMapping("/listHistory")
    @ResponseBody
    public TableDataInfo listHistory(@RequestParam Map map) {
        SysUser user = ShiroUtils.getSysUser();
        List<Authority> authorityList = authorityService.getAuthorityUser(user.getUserId().toString());
        int isAdmin = 0;
        if (authorityList.size() == 1) {
            if (authorityList.get(0).getIsAdmin() != null) {
                isAdmin = 1;
            }
        }
        startPage();
        List<Map<String, Object>> maps = salaryOpenService.findDataHistory(map, authorityList, isAdmin);
        return getDataTable(maps);
    }

    /**
     * (确认检查,取消确认,启薪处理)
     */
    @PostMapping("/procedureRegister")
    @ResponseBody
    public String procedureRegister(String ids, String procedure) {
        return salaryOpenService.procedureRegister(ids, procedure);
    }

    @PostMapping("/updateSalary")
    @ResponseBody
    public int updateSalary(@RequestParam Map map) {
        //项目
        String[] array = map.get("benefitTypeAndXl").toString().split("@@@");
        String stdId = array[1];
        map.put("stdId", stdId);
        return salaryOpenService.updateSalary(map);
    }
}
